package com.czjtxy.extraTerm.daoImpl;

import com.czjtxy.extraTerm.dao.TjobDao;
import com.czjtxy.extraTerm.entity.DO.Tjob;
import com.czjtxy.extraTerm.util.Util;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: staff-house
 * @author: MingYi
 * @create: 2021/07/01 16:12
 */
public class TjobDaoImpl extends BaseDaoImpl implements TjobDao {
    @Override
    public List<Tjob> getAll() throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "SELECT * FROM job_inf";
        ps = con.prepareStatement(sql);
        rs = ps.executeQuery();
        List<Tjob> tjobs = new LinkedList<>();
        while (rs.next()) {
            tjobs.add(conventRsToTjob(rs));
        }
        return tjobs;
    }

    @Override
    public List<Tjob> getPart(String jobName) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "SELECT * FROM job_inf WHERE `NAME` LIKE ?";//数据库定义字段不能为空，所以可以不考虑空是否需要进行匹配
        ps = con.prepareStatement(sql);
        ps.setString(1, Util.likeStr(jobName));
        rs = ps.executeQuery();
        List<Tjob> tjobs = new ArrayList<>();
        while (rs.next()) {
            tjobs.add(conventRsToTjob(rs));
        }
        return tjobs;
    }

    @Override
    public Tjob getOne(int uid) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "SELECT * FROM job_inf WHERE ID = ?";
        ps = con.prepareStatement(sql);
        ps.setInt(1, uid);
        rs = ps.executeQuery();
        Tjob tjob = null;
        if (rs.next()) {
            tjob = conventRsToTjob(rs);
        }
        return tjob;
    }

    @Override
    public void updateJob(Tjob tjob) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "UPDATE job_inf SET NAME = ? , REMARK = ? WHERE ID = ?";
        ps = conventTjobToPs(sql, tjob);
        ps.setInt(3, tjob.getId());
        ps.executeUpdate();
    }

    @Override
    public void deleteJob(int id) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "DELETE FROM job_inf WHERE ID = ?";
        ps = con.prepareStatement(sql);
        ps.setInt(1, id);
        ps.executeUpdate();
    }

    @Override
    public void addJob(Tjob tjob) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "INSERT INTO job_inf(NAME, REMARK) VALUES(?, ?)";
        ps = conventTjobToPs(sql, tjob);
        ps.executeUpdate();
    }

    private PreparedStatement conventTjobToPs(String sql, Tjob tjob) throws SQLException {
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, tjob.getName());
        ps.setString(2, tjob.getRemark());
        return ps;
    }

    private Tjob conventRsToTjob(ResultSet rs) throws SQLException {
        Tjob tjob = new Tjob();
        tjob.setId(rs.getInt("ID"));
        tjob.setName(rs.getString("NAME"));
        tjob.setRemark(rs.getString("REMARK"));
        return tjob;
    }
}
